package com.dmx.cep.action.contentManage;

import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.dmx.cep.entity.VodContentInfo;
import com.dmx.cep.service.ContentService;
import com.dmx.fw.web.struts2.action.BaseAction;

public class MetadataAction extends BaseAction{
	
	private ContentService contentService;
	
	private VodContentInfo metadata;
	
	private String contentId;
	
	private String genreText="";
	
	private String recomm;
//	private VodContentService  vodRSServiceProxy;
	
	
	public VodContentInfo getMetadata() {
		return metadata;
	}

	public void setMetadata(VodContentInfo metadata) {
		this.metadata = metadata;
	}

	public ContentService getContentService() {
		return contentService;
	}

	public void setContentService(ContentService contentService) {
		this.contentService = contentService;
	}

	//进入修改输入界面
	public String doEditInput(){
		
		metadata=(VodContentInfo)contentService.getObject(VodContentInfo.class, metadata.getContid());
		
		String genre=metadata.getGenre();
		
		if(genre!=null){
			String[] genreArr=genre.split(",");
			for(int i=0;i<genreArr.length;i++){
				genreText+=this.getText(genreArr[i]);
				if(i<genreArr.length-1) genreText+=",";
			}
		}
		
		
		
		return INPUT;
	}
	
	//执行修改提交
//	public String doEditSubmit() throws Exception{
//		try {
//			VodContentInfo contentInfo = (VodContentInfo) contentService
//					.getObject(VodContentInfo.class, metadata.getContid());
//			contentInfo.setTitle(metadata.getTitle());
//			contentInfo.setActor(metadata.getActor());
//			contentInfo.setDirector(metadata.getDirector());
//			contentInfo.setGenre(metadata.getGenre());
//			contentInfo.setOrigin(metadata.getOrigin());
//			contentInfo.setBitrate(metadata.getBitrate());
//			contentInfo.setExplanation(metadata.getExplanation());
//
//			setContentGenre(contentInfo, metadata.getGenre());
//
//			setContentOrigin(contentInfo);
//
//			contentService.saveObject(contentInfo);
//			
//			if(recomm!=null&&"RS2.0".equals(recomm)&&vodRSServiceProxy!=null){
//				try {
//					Vod vod= new Vod();
//					vod.setActor(contentInfo.getActor());
//					vod.setBitrate(contentInfo.getBitrate());
//					vod.setContid(contentInfo.getContid());
//					vod.setCreationDate(contentInfo.getCreationDate());
//					vod.setDirector(contentInfo.getDirector());
//					vod.setExplanation(contentInfo.getExplanation());
//					vod.setGenre(contentInfo.getGenre());
//					vod.setGenre_action(contentInfo.getGenreAction());
//					vod.setGenre_biography(contentInfo.getGenreBiography());
//					vod.setGenre_children(contentInfo.getGenreChildren());
//					vod.setGenre_comedy(contentInfo.getGenreComedy());
//					vod.setGenre_detective(contentInfo.getGenreDetective());
//					vod.setGenre_disaster(contentInfo.getGenreDisaster());
//					vod.setGenre_ethics(contentInfo.getGenreEthics());
//					vod.setGenre_fantastic(contentInfo.getGenreFantastic());
//					vod.setGenre_gangster(contentInfo.getGenreGangster());
//					vod.setGenre_history(contentInfo.getGenreHistory());
//					vod.setGenre_horror(contentInfo.getGenreHorror());
//					vod.setGenre_legendary(contentInfo.getGenreLegendary());
//					vod.setGenre_lovestory(contentInfo.getGenreLovestory());
//					vod.setGenre_martial(contentInfo.getGenreMartial());
//					vod.setGenre_other(contentInfo.getGenreOther());
//					vod.setGenre_political(contentInfo.getGenrePolitical());
//					vod.setGenre_sports(contentInfo.getGenreSports());
//					vod.setGenre_sf(contentInfo.getGenreSf());
//					vod.setGenre_suspense(contentInfo.getGenreSuspense());
//					vod.setHdFlag(contentInfo.getHdFlag());
//					vod.setLanguage(contentInfo.getLanguage());
//					vod.setOrigin(contentInfo.getOrigin());
//					vod.setOriginAmerica(contentInfo.getOriginAmerica());
//					vod.setOriginChina(contentInfo.getOriginChina());
//					vod.setOriginHomgkong(contentInfo.getOriginHongkong());
//					vod.setOriginJapan(contentInfo.getOriginJapan());
//					vod.setOriginOther(contentInfo.getOriginOther());
//					vod.setOriginTaiwan(contentInfo.getOriginTaiwan());
//					vod.setProviderId(contentInfo.getProviderId());
//					vod.setProviderName(contentInfo.getProviderName());
//					vod.setRating(contentInfo.getRating());
//					vod.setReleaseDate(contentInfo.getReleaseDate());
//					vod.setRunTime(contentInfo.getRunTime());
//					vod.setStatus(contentInfo.getStatus());
//					vod.setTitle(contentInfo.getTitle());
//					vodRSServiceProxy.addVodContent(vod);
//				} catch (SeeWProxyException e1) {
//					// TODO Auto-generated catch block
//					log.error("vod contents update for ",e1);
//				}
//			  }	
//			
//
//			this.setSuccessinfo(this.getText("info.modifySuccess"));
//			// this.setAjaxJsonUrlResult(resultUrl);
//			return "list";
//		} catch (Exception e) {
//			log.error(e);
//			GeneralException ge = GeneralExceptionHandler.Handle(e);
//			this.setErrorInfo(this.getText(ge.getMessage()));
//			return doEditInput();
//		}
//	}
	
	//进入查看页面
	public String doView(){
		
		metadata=(VodContentInfo)contentService.getObject(VodContentInfo.class, metadata.getContid());
		
		String genre=metadata.getGenre();
		
		if(genre!=null){
			String[] genreArr=genre.split(",");
			for(int i=0;i<genreArr.length;i++){
				genreText+=this.getText(genreArr[i]);
				if(i<genreArr.length-1) genreText+=",";
			}
		}
		
		
		
		return "view";
	}
	
	public String doDelete() throws Exception{
		
		try{
			contentService.deleteContent(new String[]{metadata.getContid()});
			this.setSuccessinfo(this.getText("info.operate.success"));
		}catch(Exception e){
			log.error(e);
			this.setErrorInfo(this.getText("err.system.UnknownException"));
		}
		return "list";
	}
	
	public String loadGenre() throws Exception{
		
		return "genre";
	}
	
	public String loadXmlTree() throws Exception{
		
		List<Object[]> contentTypeList=contentService.getContentTypeList();
		
		String xmlString=createXml("0",contentTypeList);
		
		HttpServletResponse res = ServletActionContext.getResponse();
		res.setContentType("text/xml;charset=UTF-8");
		res.setHeader("Cache-Control", "no-store");
		res.setHeader("Pragma", "no-cache");
		res.setDateHeader("Expires", 0);
		PrintWriter out = res.getWriter();
		out.print(xmlString);
		out.flush();
		out.close();
		
		return null;
	}
	
	/**
	 * @Description: 动态生成树时用此方法
	 * @param parentId
	 * @param tmpList
	 * @return String
	 */
	private String createXml(String parentId, List<Object[]> tmpList) {
		
		VodContentInfo content=(VodContentInfo)contentService.getObject(VodContentInfo.class, contentId);
		String genre=content.getGenre();
		
		StringBuffer outXml = new StringBuffer(
				"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
		outXml.append("<tree id=\"");
		outXml.append(parentId);
		outXml.append("\">\n");

		for (Object[] arr:tmpList) {
			outXml.append("<item text=\"");
			outXml.append(this.getText((String)arr[1]));
			outXml.append("\" id=\"");
			outXml.append((String)arr[1]);
			outXml.append("\"");
			if(genre!=null&&genre.indexOf((String)arr[1])!=-1)
				outXml.append(" checked=\"0\"");
			
			outXml.append(" child=\"1\"");

			outXml.append("></item>\n");
		}
		
		outXml.append("</tree>\n");
		return outXml.toString();
	}
	
	private void setContentGenre(VodContentInfo contentInfo,String genres){
		if(genres==null||"".equals(genres.trim())) return;
		
		contentInfo.setGenreOther(0);
		contentInfo.setGenreHorror(0);
		contentInfo.setGenreAction(0);
		contentInfo.setGenreSuspense(0);
		contentInfo.setGenreComedy(0);
		contentInfo.setGenreLovestory(0);
		contentInfo.setGenreHistory(0);
		contentInfo.setGenreSf(0);
		contentInfo.setGenreGangster(0);
		contentInfo.setGenreDisaster(0);
		contentInfo.setGenreEthics(0);
		contentInfo.setGenreBiography(0);
		contentInfo.setGenreSports(0);
		contentInfo.setGenreMartial(0);
		contentInfo.setGenrePolitical(0);
		contentInfo.setGenreDetective(0);
		contentInfo.setGenreLegendary(0);
		contentInfo.setGenreFantastic(0);
		contentInfo.setGenreChildren(0);
		
		String[] genreArr=genres.split(",");
		boolean isMatchGenre=false;
		for(String genre:genreArr){
			if("comedy".equalsIgnoreCase(genre.trim())){
				contentInfo.setGenreComedy(1);
				isMatchGenre=true;
			}
			else if("sf".equalsIgnoreCase(genre.trim())){
				contentInfo.setGenreSf(1);
				isMatchGenre=true;
			}
			else if("gangster".equalsIgnoreCase(genre.trim())){
				contentInfo.setGenreGangster(1);
				isMatchGenre=true;
			}
			else if("action".equalsIgnoreCase(genre.trim())){
				contentInfo.setGenreAction(1);
				isMatchGenre=true;
			}		
			else if("disaster".equalsIgnoreCase(genre.trim())){
				contentInfo.setGenreDisaster(1);
				isMatchGenre=true;
			}		
			else if("ethics".equalsIgnoreCase(genre.trim())){
				contentInfo.setGenreEthics(1);
				isMatchGenre=true;
			}			
			else if("biography".equalsIgnoreCase(genre.trim())){
				contentInfo.setGenreBiography(1);
				isMatchGenre=true;
			}	
			else if("sports".equalsIgnoreCase(genre.trim())){
				contentInfo.setGenreSports(1);
				isMatchGenre=true;
			}	
			else if("lovestory".equalsIgnoreCase(genre.trim())){
				contentInfo.setGenreLovestory(1);
				isMatchGenre=true;
			}
			else if("martial".equalsIgnoreCase(genre.trim())){
				contentInfo.setGenreMartial(1);
				isMatchGenre=true;
			}
			else if("horror".equalsIgnoreCase(genre.trim())){
				contentInfo.setGenreHorror(1);
				isMatchGenre=true;
			}
			else if("history".equalsIgnoreCase(genre.trim())){
				contentInfo.setGenreHistory(1);
				isMatchGenre=true;
			}
			else if("political".equalsIgnoreCase(genre.trim())){
				contentInfo.setGenrePolitical(1);
				isMatchGenre=true;
			}
			else if("detective".equalsIgnoreCase(genre.trim())){
				contentInfo.setGenreDetective(1);
				isMatchGenre=true;
			}
			else if("suspense".equalsIgnoreCase(genre.trim())){
				contentInfo.setGenreSuspense(1);
				isMatchGenre=true;
			}
			else if("legendary".equalsIgnoreCase(genre.trim())){
				contentInfo.setGenreLegendary(1);
				isMatchGenre=true;
			}
			else if("fantastic".equalsIgnoreCase(genre.trim())){
				contentInfo.setGenreFantastic(1);
				isMatchGenre=true;
			}
			else if("children".equalsIgnoreCase(genre.trim())){
				contentInfo.setGenreChildren(1);
				isMatchGenre=true;
			}else if("other".equalsIgnoreCase(genre.trim())){
				contentInfo.setGenreOther(1);
				isMatchGenre=true;
			}else{
				
			}
		}
		
		if(!isMatchGenre){
			contentInfo.setGenreOther(1);
		}
	}
	
	private void setContentOrigin(VodContentInfo content){
		
		content.setOriginChina(0);
		content.setOriginAmerica(0);
		content.setOriginJapan(0);
		content.setOriginKorea(0);
		content.setOriginTaiwan(0);
		content.setOriginHongkong(0);
		content.setOriginOther(0);
		
		String origin=content.getOrigin();
		if("china".equalsIgnoreCase(origin.trim())||"cn".equalsIgnoreCase(origin.trim())){
			content.setOriginChina(1);
		}else if("america".equalsIgnoreCase(origin.trim())||"us".equalsIgnoreCase(origin.trim())||"usa".equalsIgnoreCase(origin.trim())){
			content.setOriginAmerica(1);
		}else if("japan".equalsIgnoreCase(origin.trim())||"jp".equals(origin.trim())){
			content.setOriginJapan(1);
		}else if("korea".equalsIgnoreCase(origin.trim())){
			content.setOriginKorea(1);
		}else if("taiwan".equalsIgnoreCase(origin.trim())||"tw".equals(origin.trim())){
			content.setOriginTaiwan(1);
		}else if("hongkong".equalsIgnoreCase(origin.trim())||"hk".equals(origin.trim())){
			content.setOriginHongkong(1);
		}else{
			content.setOriginOther(1);
		}
	}

	public String getContentId() {
		return contentId;
	}

	public void setContentId(String contentId) {
		this.contentId = contentId;
	}

	public String getGenreText() {
		return genreText;
	}

	public void setGenreText(String genreText) {
		this.genreText = genreText;
	}
	
	public void setRecomm(String recomm) {
		this.recomm = recomm;
	}



}
